import psycopg2
import json

pgConnMy = {
    'host': '10.5.70.106',
    'port': 5432,
    'database': 'postgres',
    'user': 'postgres',
    'password': 'Cimcube@316'
}


def terminateBackend(pgConn, dbName='sdb'):
    conn = psycopg2.connect(**pgConn)
    query = """SELECT pg_terminate_backend(pg_stat_activity.pid)
    FROM pg_stat_activity
    WHERE datname = '%s' AND pid <> pg_backend_pid();
    """ % (dbName)
    cursor = conn.cursor()
    cursor.execute(query)
    conn.commit()
    cursor.close()


def deleteDb(pgConn, dbName='sdb_1'):
    conn = psycopg2.connect(**pgConn)
    conn.autocommit = True
    query = " DROP DATABASE IF EXISTS %s;" % (dbName)
    cursor = conn.cursor()
    cursor.execute(query)
    conn.commit()
    cursor.close()


if __name__ == '__main__':
    dbName = 'sdbdddd'
    terminateBackend(pgConnMy,dbName)
    deleteDb(pgConnMy, dbName)
